#include "common/pch.h"
#include "motion_generators/home_movement_generator.h"

#include "object/unit.h"
#include "object/creature.h"
#if 0
#include "movement/move_spline_init.h"
#endif

namespace battle::motion
{

    void HomeMovementGenerator<battle::object::Creature>::Initialize(battle::object::Creature& owner)
    {
        _setTargetLocation(owner);
    }

    void HomeMovementGenerator<battle::object::Creature>::Reset(battle::object::Creature&)
    {
    }

    void HomeMovementGenerator<battle::object::Creature>::_setTargetLocation(battle::object::Creature& owner)
    {
        if (owner.hasUnitState(UNIT_STAT_NOT_MOVE))
        {
            return;
        }

#if 0
        Movement::MoveSplineInit init(owner);
        float x, y, z, o;
        // at apply we can select more nice return points base at current movegen
        if (owner.GetMotionMaster()->empty() || !owner.GetMotionMaster()->top()->GetResetPosition(owner, x, y, z, o))
        {
            owner.GetRespawnCoord(x, y, z, &o);
        }
        init.SetFacing(o);
        init.MoveTo(x, y, z, true);
        init.SetWalk(false);
        init.Launch();

        arrived = false;
        owner.clearUnitState(UNIT_STAT_ALL_DYN_STATES);
#endif
    }

    bool HomeMovementGenerator<battle::object::Creature>::Update(battle::object::Creature& owner, const unsigned int& /*time_diff*/)
    {
#if 1
        return true;
#else
        arrived = owner.movespline->Finalized();
        return !arrived;
#endif
    }

    void HomeMovementGenerator<battle::object::Creature>::Finalize(battle::object::Creature& owner)
    {
#if 0
        if (arrived)
        {
            if (owner.GetTemporaryFactionFlags() & TEMPFACTION_RESTORE_REACH_HOME)
            {
                owner.ClearTemporaryFaction();
            }

            owner.SetWalk(!owner.hasUnitState(UNIT_STAT_RUNNING_STATE) && !owner.IsLevitating(), false);
            owner.LoadCreatureAddon(true);
            owner.AI()->JustReachedHome();
        }
#endif
    }


}